<?php

namespace common\helpers;

use common\models\base\CommonStatus;
use common\models\base\Department;
use common\models\base\Job;
use common\models\base\Project;
use common\models\base\ToolCategory;
use common\models\buss\Platform;
use common\models\member\Member;
use common\models\rbac\Action;
use common\models\rbac\Menu;
use common\models\rbac\Role;
use Yii;

class RedisHelper
{
    // 项目命名空间
    const KEY_TITLE = 'new_media';

    /**
     * 项目key
     * @param int $projectId
     * @return string
     */
    private static function getProjectKey(int $projectId): string
    {
        return self::KEY_TITLE.":project:{$projectId}";
    }

    /**
     * 获取项目
     * @param int $projectId
     * @return Project|null
     */
    public static function getProject(int $projectId): ?Project
    {
        $key = self::getProjectKey($projectId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setProject($data = Project::findOne($projectId));
        return $data;
    }

    /**
     * 设置项目
     * @param Project|null $project
     * @return void
     */
    public static function setProject(?Project $project): void
    {
        if (empty($project)) {
            return;
        }
        $key = self::getProjectKey($project->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($project));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除项目
     * @param int|array $ids
     * @return void
     */
    public static function delProject($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getProjectKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 部门key
     * @param int $departmentId
     * @return string
     */
    private static function getDepartmentKey(int $departmentId): string
    {
        return self::KEY_TITLE.":department:{$departmentId}";
    }

    /**
     * 获取部门
     * @param int $departmentId
     * @return Department|null
     */
    public static function getDepartment(int $departmentId): ?Department
    {
        $key = self::getDepartmentKey($departmentId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setDepartment($data = Department::findOne($departmentId));
        return $data;
    }

    /**
     * 设置部门
     * @param Department|null $department
     * @return void
     */
    public static function setDepartment(?Department $department): void
    {
        if (empty($department)) {
            return;
        }
        $key = self::getDepartmentKey($department->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($department));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除部门
     * @param int|array $ids
     * @return void
     */
    public static function delDepartment($ids): void
    {
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getDepartmentKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 岗位key
     * @param int $jobId
     * @return string
     */
    private static function getJobKey(int $jobId): string
    {
        return self::KEY_TITLE.":job:{$jobId}";
    }

    /**
     * 获取岗位
     * @param int $jobId
     * @return Job|null
     */
    public static function getJob(int $jobId): ?Job
    {
        $key = self::getJobKey($jobId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setJob($data = Job::findOne($jobId));
        return $data;
    }

    /**
     * 设置岗位
     * @param Job|null $job
     * @return void
     */
    public static function setJob(?Job $job): void
    {
        if (empty($job)) {
            return;
        }
        $key = self::getJobKey($job->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($job));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除岗位
     * @param int|array $ids
     * @return void
     */
    public static function delJob($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getJobKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 菜单key
     * @param int $menuId
     * @return string
     */
    private static function getMenuKey(int $menuId): string
    {
        return self::KEY_TITLE.":menu:{$menuId}";
    }

    /**
     * 获取菜单
     * @param int $menuId
     * @return Menu|null
     */
    public static function getMenu(int $menuId): ?Menu
    {
        $key = self::getMenuKey($menuId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setMenu($data = Menu::findOne($menuId));
        return $data;
    }

    /**
     * 设置菜单
     * @param Menu|null $menu
     * @return void
     */
    public static function setMenu(?Menu $menu): void
    {
        if (empty($menu)) {
            return;
        }
        $key = self::getMenuKey($menu->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($menu));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 动作key
     * @param int $actionId
     * @return string
     */
    private static function getActionKey(int $actionId): string
    {
        return self::KEY_TITLE.":action:{$actionId}";
    }

    /**
     * 获取动作
     * @param int $actionId
     * @return Action|null
     */
    public static function getAction(int $actionId): ?Action
    {
        $key = self::getActionKey($actionId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setAction($data = Action::findOne($actionId));
        return $data;
    }

    /**
     * 设置动作
     * @param Action|null $action
     * @return void
     */
    public static function setAction(?Action $action): void
    {
        if (empty($action)) {
            return;
        }
        $key = self::getActionKey($action->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($action));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 动作key
     * @param int $actionId
     * @return string
     */
    private static function getRoleKey(int $actionId): string
    {
        return self::KEY_TITLE.":role:{$actionId}";
    }

    /**
     * 获取动作
     * @param int $roleId
     * @return Role|null
     */
    public static function getRole(int $roleId): ?Role
    {
        $key = self::getRoleKey($roleId);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setRole($data = Role::findOne($roleId));
        return $data;
    }

    /**
     * 设置动作
     * @param Role|null $role
     * @return void
     */
    public static function setRole(?Role $role): void
    {
        if (empty($role)) {
            return;
        }
        $key = self::getRoleKey($role->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($role));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除角色
     * @param int|array $ids
     * @return void
     */
    public static function delRole($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getRoleKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 公用状态key
     * @param int $id
     * @return string
     */
    private static function getCommonStatusKey(int $id): string
    {
        return self::KEY_TITLE.":common_status:{$id}";
    }

    /**
     * 获取公用状态
     * @param int $id
     * @return CommonStatus|null
     */
    public static function getCommonStatus(int $id): ?CommonStatus
    {
        $key = self::getCommonStatusKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setCommonStatus($data = CommonStatus::findOne($id));
        return $data;
    }

    /**
     * 设置公用状态
     * @param CommonStatus|null $model
     * @return void
     */
    public static function setCommonStatus(?CommonStatus $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getCommonStatusKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除公用状态
     * @param int|array $ids
     * @return void
     */
    public static function delCommonStatus($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getCommonStatusKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 平台key
     * @param int $id
     * @return string
     */
    private static function getPlatformKey(int $id): string
    {
        return self::KEY_TITLE.":platform:{$id}";
    }

    /**
     * 获取平台
     * @param int $id
     * @return Platform|null
     */
    public static function getPlatform(int $id): ?Platform
    {
        $key = self::getPlatformKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setPlatform($data = Platform::findOne($id));
        return $data;
    }

    /**
     * 设置平台
     * @param Platform|null $model
     * @return void
     */
    public static function setPlatform(?Platform $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getPlatformKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除平台
     * @param int|array $ids
     * @return void
     */
    public static function delPlatform($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getPlatformKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 人员key
     * @param int $id
     * @return string
     */
    private static function getMemberKey(int $id): string
    {
        return self::KEY_TITLE.":member:{$id}";
    }

    /**
     * 获取人员
     * @param int $id
     * @return Member|null
     */
    public static function getMember(int $id): ?Member
    {
        $key = self::getMemberKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setMember($data = Member::findOne($id));
        return $data;
    }

    /**
     * 设置人员
     * @param Member|null $action
     * @return void
     */
    public static function setMember(?Member $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getMemberKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除人员
     * @param int|array $ids
     * @return void
     */
    public static function delMember($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getMemberKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }

    /**
     * 素材分类key
     * @param int $id
     * @return string
     */
    private static function getToolCategoryKey(int $id): string
    {
        return self::KEY_TITLE.":tool_category:{$id}";
    }

    /**
     * 获取素材分类
     * @param int $id
     * @return ToolCategory|null
     */
    public static function getToolCategory(int $id): ?ToolCategory
    {
        $key = self::getToolCategoryKey($id);
        $redis = Yii::$app->redis;
        if ($data = $redis->get($key)) {
            return unserialize($data);
        }

        self::setToolCategory($data = ToolCategory::findOne($id));
        return $data;
    }

    /**
     * 设置素材分类
     * @param ToolCategory|null $model
     * @return void
     */
    public static function setToolCategory(?ToolCategory $model): void
    {
        if (empty($model)) {
            return;
        }
        $key = self::getToolCategoryKey($model->id);
        $redis = Yii::$app->redis;
        $redis->set($key, serialize($model));
        $redis->expire($key, 86400 * 7);
    }

    /**
     * 删除素材分类
     * @param int|array $ids
     * @return void
     */
    public static function delToolCategory($ids): void
    {
        if (empty($ids)) {
            return;
        }
        $ids = is_array($ids) ? $ids : [$ids];
        foreach ($ids as $id) {
            $key = self::getToolCategoryKey($id);
            $redis = Yii::$app->redis;
            $redis->del($key);
        }
    }
}
